/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.gshop.domain.po.WareCatBrandPo;
import com.itttq.gshop.domain.to.CatBrandTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.pbpm.mapper.IWareCatBrandMapper;

@Service
public class WareCatBrandService {
	@Autowired
	private IWareCatBrandMapper wareCatBrandMapper;
	
	/**
	 * 根据ID获取WareCatBrandPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return WareCatBrandPo
	 */
	public WareCatBrandPo getWareCatBrand(int id) {
		return wareCatBrandMapper.findWareCatBrandById(id);
	}
	
	/**
	 * 根据条件获取WareCatBrandPo对象，并返回查询的数据总数
	 * 
	 * @param wareCatBrand
	 * @Author 迷蝶
	 * @return ResultPageTo<WareCatBrandPo>
	 */
	public ResultPageTo<WareCatBrandPo> getWareCatBrands(WareCatBrandPo wareCatBrand) {
		return new ResultPageTo<WareCatBrandPo>(wareCatBrand.getStart(), wareCatBrand.getSize(), 
				wareCatBrandMapper.findWareCatBrandCount(wareCatBrand), wareCatBrandMapper.findWareCatBrands(wareCatBrand));
	}
	
	/**
	 * 根据ID删除WareCatBrandPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delWareCatBrand(int id) {
		return wareCatBrandMapper.delWareCatBrandById(id);
	}
	
	/**
	 * 增加WareCatBrandPo对象
	 * 
	 * @param wareCatBrand
	 * @Author 迷蝶
	 */
	public Integer addWareCatBrand(WareCatBrandPo wareCatBrand) {
		return wareCatBrandMapper.addWareCatBrand(wareCatBrand);
	}
	
	/**
	 * 根据条件更新WareCatBrandPo对象
	 * 
	 * @param wareCatBrand
	 * @Author 迷蝶
	 */
	public Integer updateWareCatBrand(WareCatBrandPo wareCatBrand) {
		return wareCatBrandMapper.updateWareCatBrand(wareCatBrand);
	}
	
	/**
	 * 获取所有打开的品牌分类，并按分类为key、品牌List为值放入Map中
	 * TODO GXG 按显示顺序排序
	 * 
	 * @return
	 * @Author 迷蝶
	 * @return Map<String,List<WareCatBrandPo>>
	 */
	public Map<String, List<CatBrandTo>> getAllEnableCatBrands() {
		List<CatBrandTo> catBrands = wareCatBrandMapper.findCatBrands();
		Map<String, List<CatBrandTo>> catBrandMap = new HashMap<String, List<CatBrandTo>>();
		// 遍历品牌分类表，组装为展示的Map格式
		for(CatBrandTo wcb : catBrands) {
			// catBrands
			List<CatBrandTo> brands = catBrandMap.get(wcb.getCatName());
			if(null == brands) {
				brands = new ArrayList<CatBrandTo>();
			}
			brands.add(wcb);
			catBrandMap.put(wcb.getCatName(), brands);
		}
		return catBrandMap;
	}
}
